package cn.billy.basic.controller;

import cn.billy.basic.dto.LoginDto;
import cn.billy.basic.dto.RegisterDto;
import cn.billy.basic.dto.WechatDto;
import cn.billy.basic.exception.BusinessException;
import cn.billy.basic.service.ILoginService;
import cn.billy.basic.utils.AjaxResult;
import cn.billy.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
        private ILoginService iLoginService;

    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto) {
        try {
            // 传入前端的数据
            HashMap<String,Object> map = iLoginService.account(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！");
        }
    }

    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatDto wechatDto) {
        try {
            // 传入前端的数据
            return iLoginService.wechat(wechatDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！");
        }
    }
    ///wechat/binder

    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WechatDto wechatDto) {
        try {
            // 传入前端的数据
            HashMap<String,Object> map = iLoginService.binder(wechatDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！");
        }
    }
}
